<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-select v-model="query.commonType" clearable placeholder="类别" style="width: 250px;" class="filter-item">
          <el-option
            v-for="item in dict.common_type"
            :key="item.id"
            :label="item.label"
            :value="item.value" />
        </el-select>
        <el-select v-model="query.status" clearable placeholder="状态" style="width: 150px;" class="filter-item">
          <el-option
            v-for="item in dict.is_enable"
            :key="item.id"
            :label="item.label"
            :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="crud.toAdd"
        >
            新增
        </el-button>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!-- <crudOperation :permission="permission" /> -->
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="76%">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-row>
            <el-col :span="9">
              <el-form-item label="通用类型" prop="commonType">
              <el-select v-model="form.commonType" filterable placeholder="请选择" @change="commonTypeSelect">
                <el-option
                  v-for="item in dict.common_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="排序" prop="sort">
                <el-input v-model="form.sort" type="number"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio label="1">正常</el-radio>
                  <el-radio label="0">禁用</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row> 
          <div v-if="indexPub">
            <el-row>
              <el-col>
                <el-form-item label="标题" prop="indexTitle"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.indexTitle" />
                </el-form-item>
              </el-col>
            </el-row> 
            <el-row>
              <el-col>
                <el-form-item label="描述" prop="indexDec"
                  :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                  }">
                  <el-input v-model="form.indexDec" type="textarea"/>
                </el-form-item>
              </el-col>
            </el-row> 
          </div> 
          <div v-if="goodsList">
            <el-row>
              <el-col :span="11">
                <el-form-item label="商品标题" prop="goodImageArr"
                  :rules="[
                    {
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                    },
                  ]">
                  <MaterialList v-model="form.goodImageArr" type="image" :num="1" :width="100" :height="35"/>(888*50)
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item>
                  <el-button @click="addJWItem" type="primary">新增子列表</el-button>
                </el-form-item >
              </el-col>
            </el-row> 
            <div v-for="(item, index) in form.goodsChildList" :key="item.key" class="dynamic-box">
              <el-row>
                <el-col :span="4">
                  <el-form-item label="图片" :prop="'goodsChildList.' + index + '.goodImageChildArr'"
                    :rules="[
                        {
                            required: true,
                            message: '不为空',
                            trigger: 'blur',
                        },
                    ]">
                    <MaterialList v-model="item.goodImageChildArr" type="image" :num="1" :width="70" :height="70"/>(300*400)
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item label="名称" :prop="'goodsChildList.' + index + '.goodName'"
                    :rules="{
                        required: true,
                        message: '商品名称不能为空',
                        trigger: 'blur',
                    }">
                    <el-input v-model="item.goodName" type="text" placeholder="商品名称"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="价格" :prop="'goodsChildList.' + index + '.price'"
                    :rules="[
                        {
                            required: true,
                            message: '价格不能为空',
                            trigger: 'blur',
                        },
                    ]">
                    <el-input v-model="item.price" type="number" placeholder="价格"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="排序" :prop="'goodsChildList.' + index + '.sort'"
                    :rules="[
                        {
                            required: true,
                            message: '价格不能为空',
                            trigger: 'blur',
                        },
                    ]">
                    <el-input v-model="item.sort" type="number" placeholder="排序"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="1">
                  <el-form-item>
                    <i v-show="form.goodsChildList.length > 0" class="el-icon-delete" @click="removeDomain(item, index)"></i>
                  </el-form-item>
                </el-col>
              </el-row>
              
            </div>
          </div> 
          <!-- 评论语列表 -->
          <div v-if="commentList">
            <el-row>
              <el-col :span="3">
                <el-form-item>
                  <el-button @click="addCommentItem" type="primary">新增</el-button>
                </el-form-item >
              </el-col>
            </el-row> 
            <div v-for="(item, index) in form.commentChildList" :key="item.key" class="dynamic-box">
              <el-row>
                <el-col :span="4">
                  <el-form-item label="头像" :prop="'commentChildList.' + index + '.headImageChildArr'"
                    :rules="[
                        {
                            required: true,
                            message: '不为空',
                            trigger: 'blur',
                        },
                    ]">
                    <MaterialList v-model="item.headImageChildArr" type="image" :num="1" :width="70" :height="70"/>(40*40)
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="评论语" :prop="'commentChildList.' + index + '.commentName'"
                    :rules="{
                        required: true,
                        message: '评论语不能为空',
                        trigger: 'blur',
                    }">
                    <el-input v-model="item.commentName" maxlength="25" type="text" placeholder="评论语(最大25个字符)"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="排序" :prop="'commentChildList.' + index + '.sort'"
                    :rules="[
                        {
                            required: true,
                            message: '不能为空',
                            trigger: 'blur',
                        },
                    ]">
                    <el-input v-model="item.sort" type="number" placeholder="排序"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="1">
                  <el-form-item>
                    <i v-show="form.commentChildList.length > 0" class="el-icon-delete" @click="removeCommentDomain(item, index)"></i>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div> 

          <!-- 公司信息 -->
          <div v-if="companyInfoView">
            <el-row>
              <el-col :span="11">
                <el-form-item label="地图" prop="imageMapArr"
                  :rules="[
                    {
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                    },
                  ]">
                  <MaterialList v-model="form.imageMapArr" type="image" :num="1" :width="200" :height="100"/>(1919 *823)
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="公司名" prop="companyInfo.name"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.companyInfo.name" />
                </el-form-item>
              </el-col>
            </el-row> 
            <el-row>    
              <el-col :span="6">
                <el-form-item label="电话" prop="companyInfo.phone"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.companyInfo.phone" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="邮箱" prop="companyInfo.mailbox"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.companyInfo.mailbox" />
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-form-item label="地址" prop="companyInfo.address"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.companyInfo.address" />
                </el-form-item>
              </el-col>
            </el-row> 
            
          </div> 
          <!-- 底部信息 -->
          <div v-if="footInfoView">
            <el-row>
              <el-col :span="11">
                <el-form-item label="热线电话" prop="footInfo.hotLine"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.footInfo.hotLine" />
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="工作时间" prop="footInfo.workTime"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.footInfo.workTime" />
                </el-form-item>
              </el-col>
            </el-row> 
            <el-row>
              <el-col :span="11">
                <el-form-item label="ICP备案" prop="footInfo.icpInfo"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.footInfo.icpInfo" />
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="版权" prop="footInfo.copyRight"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.footInfo.copyRight" />
                </el-form-item>
              </el-col>
            </el-row> 
            <el-row>       
              <el-col :span="11">
                <el-form-item label="英文标语" prop="footInfo.englishMsg"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.footInfo.englishMsg" />
                </el-form-item>
              </el-col>
              <el-col :span="11">
                <el-form-item label="中文标语" prop="footInfo.chineseMsg"
                :rules="{
                        required: true,
                        message: '不为空',
                        trigger: 'blur',
                }">
                  <el-input v-model="form.footInfo.chineseMsg" />
                </el-form-item>
              </el-col>
            </el-row>  
          </div> 
          
          <!-- 数字信息 -->
          <div v-if="numberInfoView">
            <el-row>
              <el-col :span="3">
                <el-form-item>
                  <el-button @click="addNumberItem" type="primary">新增</el-button>
                </el-form-item >
              </el-col>
            </el-row> 
            <div v-for="(item, index) in form.numberList" :key="item.key" class="dynamic-box">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="标题" :prop="'numberList.' + index + '.titleName'"
                    :rules="{
                        required: true,
                        message: '标题不能为空',
                        trigger: 'blur',
                    }">
                    <el-input v-model="item.titleName" type="text" placeholder="标题"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="数字域" :prop="'numberList.' + index + '.dec'"
                    :rules="{
                        required: true,
                        message: '数字不能为空',
                        trigger: 'blur',
                    }">
                    <el-input v-model="item.dec" type="text" placeholder="数字域"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="排序" :prop="'numberList.' + index + '.sort'"
                    :rules="[
                        {
                            required: true,
                            message: '不能为空',
                            trigger: 'blur',
                        },
                    ]">
                    <el-input v-model="item.sort" type="number" placeholder="排序"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="1">
                  <el-form-item>
                    <i v-show="form.numberList.length > 0" class="el-icon-delete" @click="removeNumberDomain(item, index)"></i>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </div> 
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="submitSave">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;">
        <el-table-column show-overflow-tooltip v-if="columns.visible('commonType')" prop="commonType" label="通用类型" width="150">
          <template slot-scope="scope">
            {{ dict.label.common_type[scope.row.commonType] }}
          </template>
        </el-table-column>

        <el-table-column show-overflow-tooltip v-if="columns.visible('content')" prop="content" label="内容">
          <template slot-scope="scope">
            <label v-if="scope.row.commonType =='0'">
              {{JSON.parse(scope.row.content).indexTitle}}
            </label>
            <label v-if="scope.row.commonType =='1'">
              <a :href="JSON.parse(scope.row.content).goodsUrl" style="color: #42b983" target="_blank">
                <img :src="JSON.parse(scope.row.content).goodsUrl" alt="点击打开" class="table-img" />
              </a>
            </label>
            <label v-if="scope.row.commonType =='3'">
              {{JSON.parse(scope.row.content).name}}
            </label>
            <label v-if="scope.row.commonType =='4'">
              {{JSON.parse(scope.row.content).chineseMsg}}
            </label>

            
          </template>
        </el-table-column>

        <el-table-column v-if="columns.visible('sort')" prop="sort" label="排序" width="100"/>
        <el-table-column v-if="columns.visible('status')" prop="status" label="状态" width="80">
          <template slot-scope="scope">
            {{ dict.label.is_enable[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="添加时间" width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','syWebsiteCommonInfo:edit','syWebsiteCommonInfo:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSyWebsiteCommonInfo from '@/api/syWebsiteCommonInfo'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '', url: 'api/syWebsiteCommonInfo', sort: 'common_type,create_time,desc', crudMethod: { ...crudSyWebsiteCommonInfo }})
const defaultForm = { id: null, pid: null, commonType: null, content: null, sort: null, status: '1', createTime: null, updateTime: null,
indexTitle:'',indexDec:'' ,goodsUrl:'',goodImageArr:[],imageMapArr:[],
companyInfo:{
  mapUrl:'',
  name: '',
  phone: '',
  mailbox:'',
  address:'',
},
footInfo:{
  hotLine:'',
  workTime: '',
  icpInfo: '',
  copyRight:'',
  englishMsg:'',
  chineseMsg:'',
},
goodsChildList:[
    {
      goodImageChildArr:[],
      goodUrl:'',
      goodName: '',
      price: '',
      sort:'',
    }
  ],
  commentChildList:[
    {
      headImageChildArr:[],
      headUrl:'',
      commentName: '',
      sort:'',
    }
  ],
  numberList:[
    {
      titleName:'',
      dec:'',
      sort:'',
    }
  ],
}
export default {
  name: 'SyWebsiteCommonInfo',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['common_type', 'is_enable'],
  data() {
    return {
      numberInfoView:false,
      footInfoView:false,
      companyInfoView:false,
      commentList:false,
      goodsList:false,
      indexPub:false,
      permission: {
        add: ['admin', 'syWebsiteCommonInfo:add'],
        edit: ['admin', 'syWebsiteCommonInfo:edit'],
        del: ['admin', 'syWebsiteCommonInfo:del']
      },
      rules: {
        commonType: [
          { required: true, message: '通用类型不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'commonType', display_name: '通用类型' },
        { key: 'status', display_name: '状态' }
      ]
    }
  },
  watch: {
    "form.goodImageArr":function(val) {
      if (val) {
        this.form.goodsUrl = val.join(",");
      }
    },
    "form.imageMapArr":function(val) {
      if (val) {
        this.form.companyInfo.mapUrl = val.join(",");
      }
    },
    
  },
  methods: {
    addNumberItem() {
        this.form.numberList.push({
          titleName:'',
          dec:'',
          sort:'',
        })
    },
    removeNumberDomain(item, index) {
      const itemList = this.form.numberList.indexOf(item)
      if (itemList !== -1) {
          this.form.numberList.splice(index, 1)
      }
    },
    addCommentItem() {
        this.form.commentChildList.push({
          headImageChildArr:[],
          headUrl:'',
          commentName: '',
          sort:'',
        })
    },
    removeCommentDomain(item, index) {
      const itemList = this.form.commentChildList.indexOf(item)
      if (itemList !== -1) {
          this.form.commentChildList.splice(index, 1)
      }
    },
     // 新增经纬度表单项
     addJWItem() {
        this.form.goodsChildList.push({
          goodImageChildArr:[],
          goodUrl:'',
          goodName: '',
          price: '',
          sort:'',
        })
    },
     // 点击删除经纬度项
    removeDomain(item, index) {
      const itemList = this.form.goodsChildList.indexOf(item)
      if (itemList !== -1) {
          this.form.goodsChildList.splice(index, 1)
      }
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.commonType
        delete this.crud.params.status
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.commonTypeSelect();
      this.form.status = form.status.toString();
      if (form.content && form.id) {
        let type = this.form.commonType;
        if(type == '0'){
          let dataJson = JSON.parse(form.content);
          this.form.indexDec = dataJson.indexDec;
          this.form.indexTitle = dataJson.indexTitle;
        }else if(type == '1'){
          let dataJson = JSON.parse(form.content);
          if (dataJson.goodsUrl) {
            this.form.goodImageArr = dataJson.goodsUrl.split(",");
          }else{
            this.form.goodImageArr=[]
          }
          if (dataJson.goodsChild) {
            this.form.goodsChildList = dataJson.goodsChild;
          }
        }else if(type == '2'){
          let dataJson = JSON.parse(form.content);
          if (dataJson) {
            this.form.commentChildList = dataJson;
          }
        }else if(type == '3'){
          let dataJson = JSON.parse(form.content);
          if (dataJson) {
            if (dataJson.mapUrl) {
              this.form.imageMapArr = dataJson.mapUrl.split(",");
            }else{
              this.form.imageMapArr=[]
            }
            this.form.companyInfo = dataJson;
          }
        }else if(type == '4'){
          let dataJson = JSON.parse(form.content);
          if (dataJson) {
            this.form.footInfo = dataJson;
          }
        }else if(type == '5'){
          let dataJson = JSON.parse(form.content);
          if (dataJson) {
            this.form.numberList = dataJson;
          }
        }
        
      }
    },
    commonTypeSelect(){
      let type = this.form.commonType;
      if(type == '0'){
        this.indexPub = true;
        this.goodsList = false;
        this.commentList = false;
        this.companyInfoView = false;
        this.footInfoView = false;
        this.numberInfoView = false;
      }else if(type == '1'){
        this.indexPub = false;
        this.goodsList = true;
        this.commentList = false;
        this.companyInfoView = false;
        this.footInfoView = false;
        this.numberInfoView = false;
      }else if(type == '2'){
        this.indexPub = false;
        this.goodsList = false;
        this.companyInfoView = false;
        this.footInfoView = false;
        this.numberInfoView = false;
        this.commentList = true;
      }else if(type == '3'){
        this.indexPub = false;
        this.goodsList = false;
        this.commentList = false;
        this.footInfoView = false;
        this.numberInfoView = false;
        this.companyInfoView = true;
      }else if(type == '4'){
        this.indexPub = false;
        this.goodsList = false;
        this.commentList = false;
        this.companyInfoView = false;
        this.numberInfoView = false;
        this.footInfoView = true;
      }else if(type == '5'){
        this.indexPub = false;
        this.goodsList = false;
        this.commentList = false;
        this.companyInfoView = false;
        this.footInfoView = false;
        this.numberInfoView = true;
      }else{
        this.indexPub = false;
        this.goodsList = false;
        this.commentList = false;
        this.companyInfoView = false;
        this.footInfoView = false;
        this.numberInfoView = false;
      }  
    },
    submitSave(){
      this.crud.submitCU();
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 200px;
    height: 32px;
    line-height: 32px;
  }
</style>
